package com.wnnight.wys.web.config;

import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wnnight.wys.entity.config.ChargeAccount;
import com.wnnight.wys.entity.config.ChargeConfig;
import com.wnnight.wys.entity.config.PageBean;
import com.wnnight.wys.excelHandler.ChargeConfigHandler;
import com.wnnight.wys.groupInterface.AddGroup;
import com.wnnight.wys.groupInterface.AlterGroup;
import com.wnnight.wys.service.config.ChargeConfigService;
import com.wnnight.wys.vo.ResultVo;
import com.wnnight.wys.vo.config.ChargeConfigVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @ClassName: ChargeConfigController
 * @Description: //收费配置Controller
 * @author: LZJ
 * @date: 2022/8/29-23:19
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/v1/config/chargeConfig")
@Slf4j
public class ChargeConfigController {
    @Autowired
    private ChargeConfigService chargeConfigService;

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('config:chargeConfig:add')")
    public ResultVo<String> add(@RequestBody @Validated(AddGroup.class) ChargeConfig chargeConfig) {
        chargeConfigService.save(chargeConfig);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('config:chargeConfig:update')")
    public ResultVo<String> update(@RequestBody @Validated(AlterGroup.class) ChargeConfig chargeConfig) {
        chargeConfigService.update(chargeConfig);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('config:chargeConfig:delete')")
    public ResultVo<String> delete(@PathVariable int id) {
        chargeConfigService.removeById(id);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/list")
    @PreAuthorize("hasAuthority('config:chargeConfig:list')")
    public ResultVo<PageBean<ChargeAccount>> list(@RequestBody @Validated ChargeConfigVo chargeConfigVo) {
        if (chargeConfigVo.getChargeConfig().getStatus() == "" || chargeConfigVo.getChargeConfig().getStatus() == "null") {
            chargeConfigVo.getChargeConfig().setStatus(null);
        }
        PageBean<ChargeConfig> pageBean = chargeConfigService.selectPageBean(chargeConfigVo);
        return new ResultVo(pageBean);
    }

    @PostMapping("/export")
    @PreAuthorize("hasAuthority('config:chargeConfig:export')")
    public void export(@RequestBody @Validated ChargeConfigVo chargeConfigVo, HttpServletResponse response) throws IOException {
        PageBean<ChargeConfig> pageBean = chargeConfigService.selectPageBean(chargeConfigVo);
        log.debug("data:{}", pageBean.getData());
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("收费配置数据", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ChargeConfig.class).registerWriteHandler(new ChargeConfigHandler()).autoCloseStream(Boolean.FALSE).sheet("模板")
                    .doWrite(pageBean.getData());
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.writeValue(response.getWriter(), ResultVo.ERROR);
            response.getWriter().flush();
            response.getWriter().close();
        }
    }

    @PostMapping("getById/{id}")
    public ResultVo<ChargeAccount> getById(@PathVariable int id) {
        ChargeConfig chargeConfig = chargeConfigService.getById(id);
        return new ResultVo(chargeConfig);
    }
}
